package com.gec.system.exception;


import com.gec.system.util.Result;
import com.gec.system.util.ResultCodeEnum;
//import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.file.AccessDeniedException;

@ControllerAdvice
public class GlobalException  {

    // 全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e)
    {
        System.out.println(" public Result error(Exception e)...................");
        e.printStackTrace();
        return Result.fail().message("执行了全局异常..");
    }
    // 特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e)
    {
        System.out.println("public Result error(ArithmeticException e)..............");
        e.printStackTrace();
        return Result.fail().message("执行了特定异常...");
    }
    // 自定义异常
    @ExceptionHandler(MyCustomerException.class)
    @ResponseBody
    public Result  error(MyCustomerException e)
    {
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }
    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.fail().code(ResultCodeEnum.PERMISSION.getCode()).message("没有当前操作权限");
    }
}
